//
//  HomeCollectionViewCell1.swift
//  fruit-helper-swift
//
//  Created by 赵隆杰 on 2022/6/13.
//

import UIKit
import SnapKit

class HomeCollectionViewCell: UICollectionViewCell {
    private var cellImage: UIImageView!
    private var cellTitle : UILabel!
    
    var model: BookModel? {
        didSet {
            if let imageStr = model?.imageStr {
                if let typeStr = model?.imageType {
                    if Int(typeStr) == 1 {
                        if let savedImage = String.convertStrToImage(imageStr) {
                            cellImage.image = savedImage
                        }
                    } else {
                        cellImage.image = UIImage(named: imageStr)
                    }
                }
            }
            cellTitle.text = model?.book_title
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        cellImage = UIImageView()
        cellImage.layer.cornerRadius = 5
        cellImage.layer.masksToBounds = true
        cellImage.contentMode = .scaleAspectFill
        self.addSubview(cellImage)
        cellImage.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(5)
            make.centerX.equalToSuperview()
            make.width.equalTo(cellImage.snp.height).multipliedBy(640.0/749.0)
        }
        
        cellTitle = UILabel()
        cellTitle.font = UIFont.boldSystemFont(ofSize: 15)
        cellTitle.textColor = kColor333333
        cellTitle.textAlignment = .center
        self.addSubview(cellTitle)
        cellTitle.snp.makeConstraints { make in
            make.top.equalTo(cellImage.snp.bottom).offset(5)
            make.left.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
